package process

import (
	"encoding/json"
	"fmt"
	"go_code/chatroom/client/message"
	"go_code/chatroom/client/model"
)

var onlineUsers map[int64]*model.User = make(map[int64]*model.User)
var currentUser *model.CurrentUser

func outputOnlineUsers() {
	fmt.Println("显示在线用户列表...")
	for userId, _ := range onlineUsers {
		fmt.Println("用户 ID\t", userId)
	}
}

func updateUserStatus(msg *message.Message) {
	var userStatus *message.UserStatusMessage
	var user *message.User
	_ = json.Unmarshal([]byte(msg.Data), &userStatus)

	user = &userStatus.User

	// user 不存在时，初始化一个 user
	// user 存在时，只需更新在线状态
	u, ok := onlineUsers[user.UserId]
	if !ok {
		u = &model.User{
			UserId: user.UserId,
		}
	}
	u.Status = user.Status

	onlineUsers[user.UserId] = u
}


